package com.vote.www.controller;

import com.alibaba.fastjson.JSONObject;
import com.vote.www.utils.CommonHttp;
import org.apache.commons.lang.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tangwj on 2017/5/12.
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
//        Map<String, Object> token = CommonHttp.send("http://morefun.my/api/getT.do", "get", null);
//        String tk = ((Map<String,String>)token.get("result")).get("tk");
        String key = "abc123effaaee";
        Long now =  System.currentTimeMillis();
        System.out.println(now);
        now = new Long("1494905200267");
//        1494905200267
//        1495250967822
        String md5source = now + key;
        String newSignmsg = crypt(md5source);
        Map<String, String> map = new HashMap<String, String>();
        map.put("currtime",String.valueOf(now));
        map.put("cusId","1001");//1001对应abc123effaaee；1002对应
        map.put("signmsg",newSignmsg);
        map.put("appUserId","1HqpsMSMl5jiMRHfIJQ");
//        map.put("tk",tk);
//        map.put("tk","xoYC21slEMSwZJj7fUU");
        map.put("tk","EM7koZGVH8D4tmuj59K");
        System.out.println(JSONObject.toJSONString(map));
        Map<String, Object> result = CommonHttp.send("http://morefun.my/api/appUserInfo.do", "post", map);
        System.out.println(JSONObject.toJSONString(result));
    }

    private static String crypt(String md5source) {
        if (StringUtils.isBlank(md5source)) {
            throw new IllegalArgumentException("String to encript cannot be null or zero length");
        }
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(md5source.getBytes());
            byte[] hash = md.digest();
            for (int i = 0; i < hash.length; i++) {
                if ((0xff & hash[i]) < 0x10) {
                    hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }
}
